<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Cookie;


class ChangeLang
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $url = explode('/', $request->route()->uri());
        if ($url[0] != config('app.htai_namspace')){
            if (Cookie::has('language') && in_array(Cookie::get('language'), Config::get('app.locales'))){
                App::setLocale(Cookie::get('language'));
            }else{
                App::setLocale(Config::get('app.locale'));
            }
        }
        return $next($request);
    }
}
